<template>
  <div class="employees">
    <el-card>
      <div style="text-align: right">
        <el-button
          type="primary"
          size="small"
          @click="$router.push('/employees/import')"
        >导入</el-button>
        <!-- 导出所有的员工 -->
        <el-button
          type="primary"
          size="small"
          @click="exportUser"
        >导出</el-button>
        <el-button
          type="primary"
          size="small"
          @click="addEvent"
        >+ 新增员工</el-button>
        <!-- 新增员工
        1：封装一个弹框组件
        2：在员工页面使用弹框组件
        3：点击新增打开弹框，弹框组件内部可以关闭
        4：完成布局
         -->
      </div>
    </el-card>
    <el-card style="margin-top: 15px">
      <el-table :data="list" border>
        <el-table-column label="序号" type="index" width="60px" />
        <el-table-column label="姓名" sortable prop="username" />
        <el-table-column label="手机号" sortable prop="mobile" />
        <el-table-column label="工号" sortable prop="workNumber" />

        <el-table-column
          :formatter="formatterType"
          label="聘用形势"
          sortable
          prop="formOfEmployment"
        >
          <template v-slot="{ row }">
            <!-- 过滤器 -->
            <div>
              <!-- {{ row.formOfEmployment === 1 ? '正式' : '非正式' }} -->
              {{ row.formOfEmployment | formatType }}
            </div>
          </template>
        </el-table-column>

        <el-table-column label="部门" sortable prop="departmentName" />
        <!-- 自定义解决时间转换 -->
        <el-table-column label="入职时间" sortable prop="timeOfEntry">
          <template v-slot="{ row }">
            <div v-formatTime="row.timeOfEntry" />
          </template>
        </el-table-column>
        <el-table-column label="是否在职" sortable prop="enableState">
          <template v-slot="{ row }">
            <div>
              {{ row.enableState | formatState }}
            </div>
          </template>
        </el-table-column>

        <!-- <el-table-column label="状态" sortable></el-table-column> -->
        <el-table-column label="操作" width="300px">
          <template v-slot="{ row }">
            <el-button type="text" @click="look(row.id)">查看</el-button>
            <el-button type="text">转正</el-button>
            <el-button type="text">调岗</el-button>
            <el-button type="text">离职</el-button>
            <el-button type="text">角色</el-button>
            <el-button type="text" @click="del(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页导航 -->
      <div style="text-align: right; padding-top: 20px">
        <el-pagination
          :current-page="page.page"
          :page-sizes="[1, 5, 100, 200, 300]"
          :page-size="page.size"
          layout="total,sizes, prev, pager, next,jumper"
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <!-- 弹框组件 -->
    <Add ref="add" v-model="show" />
  </div>
</template>
<script>
import { sysUser } from '@/api/employees'
import employeesData from '@/api/constant/employees.js'
import Add from '@/views/employees/Add.vue'
import { sysUserDelete } from '@/api/employees'
import cookieJs from 'js-cookie'
import { export_json_to_excel } from '@/vendor/Export2Excel'
import moment from 'moment'
export default {
  components: {
    Add
  },
  //  过滤器
  filters: {
    //  聘用形式
    formatType(id) {
      // find:找出满足条件的项，如果找不到就返回undefined,找到了就返回相应的数据
      const result = employeesData.hireType.find((item) => item.id === +id)
      return result ? result.value : '-'
    },
    //  在职状态
    formatState(id) {
      const result = employeesData.workingState.find((item) => +item.id === +id)
      return result ? result.value : '-'
    }
  },
  data() {
    return {
      show: false, //  控制弹框组件与隐藏
      list: [{}],
      page: {
        page: 1, //  是  1  页码
        size: +cookieJs.get('employees-size') || 3, //  是  10  每页条数
        total: 100
      }
    }
  },
  created() {
    this.getData()
  },

  methods: {
    //  页容量切换
    handleSizeChange(size) {
      console.log('页容量', size)
      this.page.size = size
      cookieJs.set('employees-size', size)
      this.getData()
    },
    //  切换页码
    handleCurrentChange(page) {
      console.log('页码', page)
      this.page.page = page
      this.getData()
    },
    //  员工列表获取
    async getData() {
      const res = await sysUser(this.page)

      console.log('员工列表', res)
      this.list = res.data.rows
      this.page.total = res.data.total
    },

    // 通过table自带的格式化方法转换
    formatterType(row, column, cellValue, index) {
      // console.log(row, column, cellValue, index)
      const result = employeesData.hireType.find(
        (item) => item.id === cellValue
      )
      return result ? result.value : '-'
    },

    // 新增按钮绑定点击事件，点击事件中点击时打开弹框
    addEvent() {
      // this.$refs.add.isShow = true
      // 打开弹框，新增按钮点击事件处理

      this.show = true
    },
    //  删除点击事件
    // 删除点击事件
    del(id) {
      this.$confirm('您确定删除该员工吗？', '提示')
        .then(async() => {
          await sysUserDelete(id)
          // 提示
          this.$message.success('删除成功')
          // 刷新列表数据
          // 判断当前页码是否>1 判断当前页数据是否只用一条
          if (this.page.page > 1 && this.list.length <= 1) {
            this.page.page--
          }
          this.getData()
        })
        .catch(() => {})
    },
    //  员工导出按钮功能
    async exportUser() {
      const res = await sysUser({
        page: 1,
        size: 9999
      })
      console.log('员工导出列表', res.data.rows)

      const rules = {
        姓名: 'username',
        工号: 'workNumber',
        手机号: 'mobile',
        转正时间: 'correctionTime',
        部门: 'departmentName',
        是否在职: 'enableState',
        聘用形势: 'formOfEmployment'
      }

      const newArr = res.data.rows.map((item) => {
        const arr = []
        Object.values(rules).forEach((item2) => {
          if(item2==='correctionTime'){
           arr.push( moment(item[item2]).format('YYYY-MM-DD'))

          }else{
            arr.push(item[item2])
          }
         
        })
        return arr
      })

      export_json_to_excel({
        header: Object.keys(rules),
        data: newArr,
        filename: '员工列表'
      })
    },

    //  员工查看页面
    look(id) {
      this.$router.push('/employees/userlook/' + id)
    }
  }
}
</script>
<style lang="scss" scoped>
.employees {
  padding: 15px;
}
</style>
